# coding:utf-8
"""
description:Countdown页面
"""
import os
import random
import time

from appium.webdriver.common import mobileby

from common.Get_Yaml import get_yaml
from common.Log import LOG, logger
from config.GlobalParameter import TEST_Element_YAML
from pages.Devices.ModeSchedule_page import ModeSchedulePublic_page

Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'Countdown.yaml'))


class Countdown_page(ModeSchedulePublic_page):
    by = mobileby.MobileBy()

    """countdown list page》获取countdown time"""
    CountdownTime_loc = (by.ID, Data.get_elementInfo(0))
    """countdown list page》获取countdown Action"""
    CountdownAction_loc = (by.ID, Data.get_elementInfo(1))
    """countdown list page》获取countdown switch,点击或者文字（ON，Off）"""
    switchCountdown_loc = (by.ID, Data.get_elementInfo(2))

    def get_CountdownTime(self):
        """countdown list page》获取countdown time"""
        return self.find_elements(*self.CountdownTime_loc)

    def get_CountdownAction(self):
        """
        countdown list page》获取countdown Action数量，如果没有数据则返回0
        :return:
        """
        if self.assertVisible(*self.CountdownAction_loc):
            return len(self.find_elements(*self.CountdownAction_loc))
        else:
            return 0

    def get_switchCountdown(self):
        """countdown list page》获取countdown switch,点击或者文字（ON，Off）"""
        return self.find_elements(*self.switchCountdown_loc)

    """Add countdown page》save按钮"""
    AddCountdownSave_loc = (by.ID, Data.get_elementInfo(3))
    """Add countdown page》On 按钮"""
    btnAddCountdownOn_loc = (by.ID, Data.get_elementInfo(4))
    """Add countdown page》Off 按钮"""
    btnAddCountdownOff_loc = (by.ID, Data.get_elementInfo(5))

    def click_AddCountdownSave(self):
        """Add countdown page》save按钮"""
        self.find_element(*self.AddCountdownSave_loc).click()

    def click_btnAddCountdownOn(self):
        """Add countdown page》On 按钮"""
        self.find_element(*self.btnAddCountdownOn_loc).click()

    def click_btnAddCountdownOff(self):
        """Add countdown page》Off 按钮"""
        self.find_element(*self.btnAddCountdownOff_loc).click()

    """Edit countdown page》save按钮"""
    EditCountdownSave_loc = (by.ID, Data.get_elementInfo(6))
    """Edit countdown page》On按钮"""
    btnEditCountdownOn_loc = (by.ID, Data.get_elementInfo(7))
    """Edit countdown page》Off 按钮"""
    btnEditCountdownOff_loc = (by.ID, Data.get_elementInfo(8))
    """Edit countdown page》Delete 按钮"""
    btnEditCountdownDel_loc = (by.ID, Data.get_elementInfo(9))

    def click_EditCountdownSave(self):
        """Edit countdown page》save按钮"""
        self.find_element(*self.EditCountdownSave_loc).click()

    def click_btnEditCountdownOn(self):
        """Edit countdown page》On按钮"""
        self.find_element(*self.btnEditCountdownOn_loc).click()

    def click_btnEditCountdownOff(self):
        """Edit countdown page》Off 按钮"""
        self.find_element(*self.btnEditCountdownOff_loc).click()

    def click_btnEditCountdownDel(self):
        """Edit countdown page》Delete 按钮"""
        self.find_element(*self.btnEditCountdownDel_loc).click()

    def func_AddCountdown(self, message):
        """
        添加，随机选择power 断言添加成功的数量+1
        :return:
        """
        self.click_btnDeviceSchedulingAdd()  # 点击Countdown list右上角+号
        self.click_AddCountdownSave()
        return self.find_toast(message)

    def func_AddCountdown2(self):
        """
        添加，滑动hour，随机选择power 断言添加成功的数量+1
        :return:
        """
        num = self.get_CountdownAction()  # 新增前的数量
        self.click_btnDeviceSchedulingAdd()  # 承接上面Add Countdown1方法，直接Countdown list右上角+号
        super().swipe_NumberPicker_Text(3)  # 滑动hour
        if random.randint(1, 2) == 1: self.click_btnAddCountdownOff()
        self.click_AddCountdownSave()
        return num + 1 == len(self.get_switchCountdown())

    def func_EditCountdown(self):
        """
        只修改time》minute为下一分钟，设备倒计时一分钟结束后，检查Countdown page该项switch为OFF
        :return:
        """

        if super().assert_SchedulingAddedList():
            for index, row in enumerate(super().get_CountdownAction()):
                # super().get_switchCountdown()[index].click()  # 关闭倒计时
                # LOG.info(
                #     "关闭Countdown,switch 的checked属性为：%s" % super().get_switchCountdown()[index].get_attribute("checked"))
                row.click()  # 直接根据第一个进行编辑，跳转edit page
                # 滑动hour，为0小时.minute自动为1min
                super().swipe_NumberPicker_Text(0)
                # 滑动minute.倒计时为2分钟
                # super().swipe_NumberPicker_Text(2, indexs=1)
                self.click_EditCountdownSave()
                # super().get_switchCountdown()[index].click()  # 重新打开倒计时
                time.sleep(70)
                super().swipe_listPage()
                LOG.info("编辑Countdown倒计时1min，等待70s后，关闭switch。实际为%s》》》" % super().get_switchCountdown()[index].text)
                return super().get_switchCountdown()[index].text == "OFF"
        else:
            LOG.info("未找到可编辑的Countdown项，请检查Countdown list page数据》》》")
            return False

    def func_DelCountdown(self, message):
        """
        在item左滑或者编辑页点击delete按钮进行删除操作,断言提示内容
        :param message:
        :return: False
        """
        self.click_ControlCountdown()

        if super().assert_SchedulingAddedList():
            for i in enumerate(super().get_switchCountdown()):
                if random.randint(1, 2) == 1:
                    LOG.info("删除操作。左滑动列表项点击delete按钮进行删除》》》")
                    super().leftSwipeClickDel(self.get_switchCountdown(), self.get_CountdownTime())  # 随机选择item点击delete
                    super().click_confirm()
                else:
                    LOG.info("删除操作。进入列表项详情页点击底部delete按钮进行删除》》》")
                    random.choice(self.get_CountdownTime()).click()  # 随机选择item进入edit page
                    self.click_btnEditCountdownDel()
                    super().click_confirm()
                self.find_toast(message)
            return super().assert_SchedulingAddedList()
        else:
            LOG.info("未找到可编辑的Countdown项，请检查Countdown list page数据》》》")
            return False
